<?php
class Dig_Service_Array {
    public static function extendArray() {
        // Holds all the arrays passed
        $params = & func_get_args ();

        // First array is used as the base, everything else overwrites on it
        $return = array_shift ( $params );

        // Merge all arrays on the first array
        foreach ( $params as $array ) {
            foreach ( $array as $key => $value ) {
                // Numeric keyed values are added (unless already there)
                if (is_numeric ( $key ) && (! in_array ( $value, $return ))) {
                    if (is_array ( $value )) {
                        $return [] = self::extendArray ( $return [$key], $value );
                    } else {
                        $return [] = $value;
                    }

                // String keyed values are replaced
                } else {
                    if (isset ( $return [$key] ) && is_array ( $value ) && is_array ( $return [$key] )) {
                        $return [$key] = self::extendArray ( $return [$key], $value );
                    } else {
                        $return [$key] = $value;
                    }
                }
            }
        }

        return $return;
    }
}